var goodsRiskCtrl  = new XFApp.client();

XFApp.client.prototype.goodsRisk = {
		successCode : "10000",	
		riskWarningPage : null,
		subGoodsPage : null,
		checkCodes:[],
		waitSubCodes:[],
		urlItems : {
			initListURL: basePath+"/goods/goodsRisk/queryGoodsRiskPage",			
			loadGoodsCategoryURL:basePath+"/goods/goodsRisk/loadGoodsCategoryList",
			queryGoodsCategoryPageURL:basePath+"/goods/goodsRisk/queryGoodsCategoryPage",
			improveRiskURL:basePath+"/goods/goodsRisk/improveRisk",
			resetRiskURL:basePath+"/goods/goodsRisk/downgradeRiskLevel"
		},
		
		load : function (){
			var _this = this;
			if(!!_this.riskWarningPage){
				_this.riskWarningPage.page("remote",0,null);
			}else{
				_this.riskWarningPage = $("#page").page({
				pageSize : 10,
				remote: {
					url: _this.urlItems.initListURL,
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){			        		
			        		if(null == data 
									|| null == data.result 
										|| null == data.result.itemList
											|| data.result.itemList.length < 1){
			        			
			        			$("#data_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}else{
			        			var pageResult = data.result;
			        			for(var index in pageResult.itemList){
			        				var element = pageResult.itemList[index];
			        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
			        			}
			        			$("#data_grid").html($("#dataTmp").render(pageResult.itemList));
			        		}			        		
			        	}else{
			        		window.webDialogUtil.alert("提示",data.msg);			        		   
			        	}
			        },
			        error : function(){			        	
			        	window.webDialogUtil.alert("提示","数据查询失败!");			      	  
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});	
		   }			
		},
		/**
		 * 查询分类列表
		 */
		querySubPage : function (belongCode,goodsName){
			var _this = this;
			var code = $("#code").val();
			var name = $("#nameFuzzy").val();
			if(!code && !name &&!belongCode){
				window.webDialogUtil.alert("提示","请输入查询条件");		
				return ;
			}
			if(!!goodsName){
				$("#goods_type_txt").text(goodsName);
			}else{
				$("#goods_type_txt").text("");
			}
			var objParams = {
				code:code,
				nameFuzzy:name,
				belongCode:belongCode
			}
			if(!!_this.subGoodsPage){
				_this.subGoodsPage.page("remote",0,objParams);
			}else{
				_this.subGoodsPage = $("#page2").page({
				pageSize : 10,
				remote: {
					url: _this.urlItems.queryGoodsCategoryPageURL,
					params: objParams, 
			        success: function (data, pageIndex) {
			        	if(data.code==_this.successCode){
			        		$("#tree_div").hide();
			        		$("#serach_result_div").show();
			        		if(null == data 
									|| null == data.result 
										|| null == data.result.itemList
											|| data.result.itemList.length < 1){
			        			
			        			$("#data_grid2").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
			        		}else{
			        			var pageResult = data.result;
			        			for(var index in pageResult.itemList){
			        				var element = pageResult.itemList[index];
			        				element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
			        			}
			        			$("#data_grid2").html($("#dataTmp2").render(pageResult.itemList));
			        		}			        		
			        	}else{
			        		window.webDialogUtil.alert("提示",data.msg);			        		   
			        	}
			        },
			        error : function(){			        	
			        	window.webDialogUtil.alert("提示","数据查询失败!");			      	  
			        }
				},
				firstBtnText : '首页',
				lastBtnText : '尾页',
				prevBtnText : '上一页',
				nextBtnText : '下一页',
				showInfo : true,
				showJump : true,
				jumpBtnText : '跳转',
				showPageSizes : true,
				infoFormat : '共{pageCount}页'
			});	
		   }			
		},
		loadGoodsTree:function(){
			var _this = this;
			var treeDate = [];
			$.ajax({
				url:_this.urlItems.loadGoodsCategoryURL,
				dataType:'json',
				type:'post',
				success:function(data){
					if(data.code!=10000){
						webDialogUtil.simpleAlert(data.msg, "温馨提示"); 
					}else{
						var goodsData = data.result;
						for(var index = 0;index < goodsData.length; index++){
							var goods = goodsData[index];
							var treeNode = {};
							treeNode.id=goods.code;
							treeNode.text = goods.name;
							var checked = goods.riskLevel.index==1?false:true;
							if(checked){
								_this.checkCodes.push(goods.code);
							}
							treeNode.state = {
									expanded:false,
									checked:checked,
							};
							treeNode.nodes = [];
							var subList = goods.subGoodsCategorys;
							for(var subIndex = 0 ;subIndex < subList.length; subIndex++){
								var subGoods = subList[subIndex];
								var subTreeNode = {};
								subTreeNode.text = subGoods.name;
								subTreeNode.id=subGoods.code;
								var checked = subGoods.riskLevel.index==1?false:true;
								if(checked){
									_this.checkCodes.push(subGoods.code);
								}
								subTreeNode.state = {
										expanded:false,
										checked:checked,
								};
								subTreeNode.nodes = [];
								treeNode.nodes.push(subTreeNode);
							}
							treeDate.push(treeNode);
						}
						$('#goodsTree').treeview({
							data: treeDate,
							showCheckbox:true,
							showTags:true
						}).on('nodeChecked', function(event, node) {
							_this.treeCheck(event, node);
						}).on('nodeUnchecked', function(event, node) {
							_this.treeUnCheck(event, node);
						}).on('nodeExpanded ',function(event,node){
							_this.expandTree(event, node);
						});   
					}
				},
				error:function(){
					webDialogUtil.simpleAlert("商品分类数据查询异常", "温馨提示"); 
				}
			});
		},
		
		treeCheck:function(event, node){
			if($.inArray(node.id, _this.checkCodes) == -1){
				_this.waitSubCodes.push(node.id);
				if(node.nodes !=undefined && node.nodes instanceof Array){
					for(var index = 0;index<node.nodes.length;index++){
						var subNode = node.nodes[index];
						$('#goodsTree').treeview('checkNode', [ subNode.nodeId, { silent: true } ]);
					}
				}
			}
		},
		treeUnCheck:function(event, node){
			if($.inArray(node.id, _this.checkCodes) != -1){
				$('#goodsTree').treeview('checkNode', [ node.nodeId, { silent: true } ]);
			}else{
				for(var index = 0 ;index<_this.waitSubCodes.length;index++){
					if(_this.waitSubCodes[index] == node.id){
						_this.waitSubCodes.splice(index,1);
						if(node.nodes !=undefined && node.nodes instanceof Array){
							for(var index = 0;index<node.nodes.length;index++){
								var subNode = node.nodes[index];
								$('#goodsTree').treeview('uncheckNode', [ subNode.nodeId, { silent: true } ]);
							}
						}
						break;
					}
				}
			}
		},
		expandTree:function(event, node){
			if(node.parentId != undefined){
				_this.querySubPage(node.id,node.text);
			}
		},
		
		/**
		 * 提高风险等级
		 */
		improveRisk:function(code){
			var _this = this;
			var codes = [];
			
			if(code == undefined){
				codes = _this.waitSubCodes;
			}else{
				codes.push(code);
			}
			if(codes.length==0){
				webDialogUtil.simpleAlert("请选择商品分类", "温馨提示"); 
				return;
			}
			webDialogUtil.simpleConfrimAlert('是否确认设置风险等级?', function(){
				var params = {codes:codes};
				$.ajax({
					url:_this.urlItems.improveRiskURL,
					dataType:'json',
					type:'post',
					data:params,
					success:function(data){
						if(data.code!=10000){
							webDialogUtil.simpleAlert(data.msg, "温馨提示"); 
						}else{
							webDialogUtil.simpleAlert("操作成功", "温馨提示"); 
							_this.load();
							_this.loadGoodsTree();
							_this.waitSubCodes=[];
							if(code != undefined){
								_this.querySubPage();
							}
						}
					},
					error:function(){
						webDialogUtil.simpleAlert("设置商品高风险等级异常", "温馨提示"); 
					}
				});
			});
		},
		
		/**
		 * 重置风险等级
		 */
		resetRisk:function(code){
			var _this = this;
			webDialogUtil.simpleConfrimAlert('是否确认重置风险等级?', function(){
				var params = {code:code};
				$.ajax({
					url:_this.urlItems.resetRiskURL,
					dataType:'json',
					type:'post',
					data:params,
					success:function(data){
						if(data.code!=10000){
							webDialogUtil.simpleAlert(data.msg, "温馨提示"); 
						}else{
							webDialogUtil.simpleAlert("操作成功", "温馨提示"); 
							_this.load();
							_this.loadGoodsTree();
						}
					},
					error:function(){
						webDialogUtil.simpleAlert("重置商品风险等级异常", "温馨提示"); 
					}
				});
			});
		},
		event : function(){
			_this = this;
			$("#btnSearch").on("click",function(){
				_this.querySubPage();
			});
			$("#back_tree").on("click",function(){
				$("#serach_result_div").hide();
				$("#tree_div").show();
			});
			$("#goods_pass_btn").on("click",function(){
				_this.improveRisk();
			});
		},
};

$(function(){
	goodsRiskCtrl.goodsRisk.load();
	goodsRiskCtrl.goodsRisk.loadGoodsTree();
	goodsRiskCtrl.goodsRisk.event();
});